package org.example.erp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.example.erp.domain.NewSalesOrder;
import org.example.erp.service.NewSalesOrderService;
import org.example.erp.web.VO.SalesOrderVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class NewSalesOrderController {
    // @RequiredArgsConstructor spring会自动构造注入
    private final NewSalesOrderService newSalesOrderService;

    // 新增主订单数据
    @PostMapping("/salesOrders/insert")
    public DataResult insertPrimaryOrder(@RequestBody NewSalesOrder newSalesOrder) {
        newSalesOrderService.insertPrimaryOrder(newSalesOrder);
        return DataResult.success();
    }

    @PutMapping("/salesOrders/update")
    public DataResult updatePrimaryOrder(@RequestBody NewSalesOrder newSalesOrder) {
        newSalesOrderService.updatePrimaryOrder(newSalesOrder);
        return DataResult.success();
    }

    @GetMapping("/salesOrders/find")
    public DataResult findSalesOrders(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(required = false) String salesOrderName,
                                      @RequestParam(required = false) String salesOrderCode) {
        IPage<SalesOrderVO> salesOrders = newSalesOrderService.findSalesOrders(pageNum, pageSize,salesOrderCode, salesOrderName);
        return DataResult.success(salesOrders);
    }
    @DeleteMapping("/salesOrders/delete")
    public DataResult deletePrimaryOrder(@RequestBody List<Integer> salesOrderIds) {
        return newSalesOrderService.deletePrimaryOrders(salesOrderIds);
    }
    @GetMapping("/salesOrders/query")
    public DataResult findSalesOrderById(@RequestParam(value = "salesOrderId") Integer salesOrderId) {
        NewSalesOrder salesOrder = newSalesOrderService.findSalesOrderById(salesOrderId);
        return DataResult.success(salesOrder);
    }
}
